import request from '/@/utils/request';

export const getDicts = (type: String) => {
  return request({
    url: `/bizs/prodProp/${type}/values`,
    method: 'get',
  });
};

export function getList(query: any) {
  return request({
    url: '/bizs/prodProp/list',
    method: 'get',
    params: query,
  });
}

export function getPropValuePage(query: any) {
  return request({
    url: '/bizs/prodProp/item/page',
    method: 'get',
    params: query,
  });
}

export function addItemObj(obj: any) {
  return request({
    url: '/bizs/prodProp/item',
    method: 'post',
    data: obj,
  });
}

export function getItemObj(id: string) {
  return request({
    url: '/bizs/prodProp/item/details/' + id,
    method: 'get',
  });
}

export function getItemDetails(obj: object) {
  return request({
    url: '/bizs/prodProp/item/details',
    method: 'get',
    params: obj,
  });
}

export function delItemObj(id: string) {
  return request({
    url: '/bizs/prodProp/item/' + id,
    method: 'delete',
  });
}

export function putItemObj(obj: any) {
  return request({
    url: '/bizs/prodProp/item',
    method: 'put',
    data: obj,
  });
}

export function addObj(obj: any) {
  return request({
    url: '/bizs/prodProp',
    method: 'post',
    data: obj,
  });
}

export function getObj(id: string) {
  return request({
    url: '/bizs/prodProp/details' ,
    method: 'get',
    params:{
      propId:id
    }
  });
}

export function getObjDetails(obj: object) {
  return request({
    url: '/bizs/prodProp/details',
    method: 'get',
    params: obj,
  });
}

export function delObj(ids: Object) {
  return request({
    url: '/bizs/prodProp',
    method: 'delete',
    data: ids,
  });
}

export function putObj(obj: any) {
  return request({
    url: '/bizs/prodProp',
    method: 'put',
    data: obj,
  });
}

export function refreshCache() {
  return request({
    url: '/bizs/prodProp/sync',
    method: 'put',
  });
}

export function validateDictType(rule: any, value: any, callback: any, isEdit: boolean) {
  if (isEdit) {
    return callback();
  }

  getObjDetails({ dictType: value }).then((response) => {
    const result = response.data;
    if (result !== null) {
      callback(new Error('属性已经存在'));
    } else {
      callback();
    }
  });
}

export function validateDictItemLabel(rule: any, value: any, callback: any, type: string, isEdit: boolean) {
  if (isEdit) {
    return callback();
  }

  getItemDetails({ dictType: type, label: value }).then((response) => {
    const result = response.data;
    if (result !== null) {
      callback(new Error('属性值已经存在'));
    } else {
      callback();
    }
  });
}
